package com.afkt.project.ui.adapter;

import com.ohos.afkt.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class TextAdapter extends BaseItemProvider {

    private List<String> mData;

    private Context context;

    public TextAdapter(List<String> data, Context context) {
        this.context = context;
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component rootView;
        ViewHolder viewHolder;
        if (component == null) {
            viewHolder = new ViewHolder();
            component = viewHolder.rootView;
            component.setTag(viewHolder);
            rootView = viewHolder.rootView;
        } else {
            viewHolder = (ViewHolder) component.getTag();
            rootView = viewHolder.rootView;
        }
        viewHolder.btn.setText(mData.get(i));
        return rootView;
    }
    private class ViewHolder {
        private Text btn;

        private Component rootView;
        public ViewHolder() {
            rootView =  LayoutScatter.getInstance(context).parse(ResourceTable.Layout_adapter_text, null, false);
            btn = (Text) rootView.findComponentById(ResourceTable.Id_vid_at_title_tv);
            btn.setClickable(false);
        }
    }
}
